(function(factory) {
	if (typeof define === "function" && define.amd) {
		// AMD. Register as an anonymous module.
		define(factory);
	} else {
		// Browser globals
		factory();
	}
}(function() {
	/**
	 * @author 李球 <service@hteos.com>
	 * @class HteOS.tile.Photo 腾讯图片新闻
	 * @constructor
	 */
	var Photo = function(el, options) {
		var photo = this;
		photo.$el = $(el);
		photo.duration = options.duration || 1000 * 60 * 10;
		photo.intervalId = window.setInterval(function() {
			photo.load();
		}, photo.duration);
		photo.load();
		photo.tpl = HteOS.Template.compile('<% for(i = 0;i < list.length; i++){ %>' + '<div class="tile-live">' + '<a href="<%=list[i].slink%>" target="_blank">' + '<img alt="" src="<%=list[i].img1%>">' + '<div class="hte-tile-text"><%=list[i].title%></div>' + '</a>' + '</div>' + '<% } %>');
	};

	Photo.prototype.start = function(){
		var photo = this;
		if (!photo.lived) {
			photo.live = new HteOS.tile.Live(photo.$el,{});
			photo.lived = true;
		} else {
			photo.live.slide();
		}
	}
	
	/**
	 * 加载图片新闻列表
	 */
	Photo.prototype.load = function() {
		var photo = this;
		HteOS.Masker.mask(photo.$el,'正在加载...');
		$.ajax({
			url: HteOS.basePath + "api/photo.json",
			dataType: "json",
			success: function(data) {
				HteOS.Masker.unmask(photo.$el);
				photo.render(data);
				photo.start();
			},
			fail: function() {
				HteOS.Masker.mask(photo.$el,'加载失败');
			}
		});

	};

	/**
	 * 渲染图片新闻
	 */
	Photo.prototype.render = function(data) {
		this.$el.find(".hte-tile-content").html(this.tpl({
			list: data
		}));
		if(HteOS.debug){
			this.$el.append($("<div class=\"hte-tile-text\">该程序仅用于测试，对外公开发生侵权问题HteOS不承担任何责任。</div>"));
		}
	};
	
	Photo.prototype.stop = function(){
		window.clearInterval(this.intervalId);
		if(this.live){
			this.live.stop();
		}
	}
	
	Photo.prototype.destroy = function(){
		this.stop();
		if(this.live){
			this.live.destroy();
		}
		this.$el = null;
	}
	
	HteOS.TileManager.register('photo',Photo);
	return Photo;
}));